package com.example.treehole.ui.page.myself

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.sp
import com.example.treehole.LocalMyselfViewModel
import com.example.treehole.LocalUserViewModel
import com.example.treehole.ui.component.PostCard
import com.example.treehole.ui.component.RefreshBox
import com.example.treehole.ui.model.PostInfo

@Composable
fun MyPosts() {
    val userViewModel = LocalUserViewModel.current
    val userId by userViewModel.userId.collectAsState()
    val jwlToken by userViewModel.jwlToken.collectAsState()

    val mySelfViewModel = LocalMyselfViewModel.current
    val postList by mySelfViewModel.myPostList.collectAsState()
    val postPage by mySelfViewModel.myPostPage.collectAsState()

    // 下拉刷新
    val isRefreshing = remember { mutableStateOf(false) }
    // 底部加载
    val isLoading = remember { mutableStateOf(false) }
    val listState = LazyListState()

    LaunchedEffect(listState) {
        snapshotFlow {
            listState.layoutInfo.visibleItemsInfo.lastOrNull()?.index == postList.size - 1
        }
            .collect { isAtBottom ->
                if (isAtBottom &&
                    !isRefreshing.value &&
                    !isLoading.value &&
                    postPage != -2
                ) {
                    userId?.let {
                        isLoading.value = true
                        mySelfViewModel.getMoreMyPostList(jwlToken, it)
                        isLoading.value = false
                    }
                }
            }
    }

    RefreshBox(
        isRefreshing = isRefreshing,
        onRefresh = {
            userId?.let {
                mySelfViewModel.refreshMyPostList(jwlToken,it)
            }
        }
    ) {
        if (postList.isNotEmpty()) {
            LazyColumn(state = listState) {
                if (!isRefreshing.value) {
                    items(postList) { item: PostInfo -> PostCard(post = item) }
                }
            }
        } else {
            Box(
                modifier = Modifier
                    .fillMaxSize()
                    .verticalScroll(rememberScrollState()),
                contentAlignment = Alignment.Center
            ) { Text(text = "去发表你的第一个帖子吧~", fontSize = 24.sp, color = Color.White) }
        }
    }

}
